package com.jt.controller;

import java.io.File;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.jt.service.FileService;
import com.jt.vo.ImageVO;

@RestController	//返回JSON串
public class FileController {
	
	@Autowired
	private FileService fileService;
	
	/**
	 * 实现文件上传之后,返回特定的数据.
	 *url地址: http://localhost:8091/file
	 * 用户提交的参数:  fileImage
	 * 返回值要求:
	 * 注意事项: 默认条件下 文件上传不能超过1M
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("/file")
	public String file(MultipartFile fileImage) throws IllegalStateException, IOException {
		//1.确定文件目录结构
		String path = "E:/JT_IMAGE/";
		
		File file = new File(path);
		//判断图片路径是否正确
		if(!file.exists()) {	//表示取非
			file.mkdirs();		//可以创建多级目录
		}
		//2.拼接文件路径信息. E:/JT_IMAGE/a.jpg
		String fileName = fileImage.getOriginalFilename();
		String filePath = path + fileName;
		
		//3.按照指定的路径上传图片
		fileImage.transferTo(new File(filePath));
		return "文件上传执行成功!!!";
	}
	
	
	/**
	 * 	图片上传实现
	 * 	1.url地址:http://localhost:8091/pic/upload?dir=image
	 * 	2.args:uploadFile
	 *  3.返回值结果: ImageVO对象
	 */
	@RequestMapping("/pic/upload")
	public ImageVO fileUpload(MultipartFile uploadFile) {
		
		return fileService.fileUpload(uploadFile);
	}
	
	
	
}
